// pages/test/searchTest.js
const app = getApp();
const QQMapWX = app.getQQMap();
const demo = new QQMapWX({
  key: 'LBOBZ-AFKCX-SDS4Z-7OY56-CPLTJ-DGBMU'
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchContent: '美食',
    page_index: 1,
    restArr: [],
    distanceInfo: [],
    count:0
  },
  getSearchContent : function (e) {
    this.setData({
      searchContent: e.detail.value
    })
  },
  btnGetSearch(){
    this.setData({
      page_index: 1,
      restArr: [],
      distanceInfo: [],
      count: 0
    })
    this.getSearch();
  },
  getSearch : function () {
    let restLocal = [];
    let that = this;
    demo.search({
      keyword: this.data.searchContent.trim()||'美食',
      page_size: 10,
      page_index: that.data.page_index,
      success: function (res) {
        console.log(res);
        let restArr = res.data;
        // console.log(restArr);
        that.setData({
          count: res.count,
          restArr: that.data.restArr.concat(restArr)
        })
        for(let i=0, len=restArr.length; i<len; i++){
          restLocal[i] = {};
          restLocal[i].latitude = restArr[i].location.lat;
          restLocal[i].longitude = restArr[i].location.lng;
        }
      },
      fail: function (res) {
        if(res.status == '1000'){
          app.showUserInfo('没有权限，请前往设置页设置', 'none', 1500)
        }
        else{
          app.showUserInfo('获取商家失败', 'none', 1000)
        }
      },
      complete: function (res) {
        that.getDistance(restLocal);
      }
    });
  },
  getDistance: function (paras) {
    let that = this;
    let latitude = this.data.latitude;
    let longitude = this.data.longitude;
    console.log('参数为', paras);
    demo.calculateDistance({
      'from': {
        latitude: latitude,
        longitude: longitude
      },
      to: paras,
      success: function (res) {
        console.log(res);
        that.setData({
          distanceInfo: that.data.distanceInfo.concat(res.result.elements)
        });
      },
      fail: function (res) {
        // console.log(res);
        that.setData({
          distanceInfo: that.data.distanceInfo.concat(new Array(paras.length).fill(0))
        })
        if(res.status === 373){
          app.showUserInfo('起终点距离过长，获取距离失败', 'none', 2000);
        }
        console.log('失败' ,res);        
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */  
  beginSearch: function () {
    this.setData({
      searchContent: '',
    })
  },
  linkToPage: function(e){
    let restArr = this.data.restArr;
    let distanceInfo = this.data.distanceInfo;
    let index = e.currentTarget.dataset.num;
    console.log(restArr[index]);
    let latitude = restArr[index].location.lat;
    let longitude = restArr[index].location.lng;
    demo.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        let cityCode = res.result.ad_info.city_code.slice(3);
        wx.navigateTo({
      url: `../restConfirm/restConfirm?title=${restArr[index].title}&distance=${~~distanceInfo[index].distance}&address=${restArr[index].address}&longitude=${longitude}&latitude=${latitude}&cityCode=${cityCode}`,
        })
      },
      fail: function (res) {
      }
    });
  },
  onLoad: function (options) {
    let local = app.getMyLocaltion();
    this.setData({
      longitude: local.longitude,
      latitude: local.latitude
    })
  },
  onReachBottom(){
    if (this.data.count <= 10 * this.data.page_index) return
    this.data.page_index++;
    this.getSearch();
  }
})